﻿using Client.Common.Extensions;
using Client.Todo.Common;
using Client.Todo.Common.Models;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Client.Todo.ViewModels
{
    /// <summary>
    /// 主界面
    /// </summary>
    public class MainViewModel:BindableBase,IConfigureService
    {
        private readonly IRegionManager _regionManager;

        private IRegionNavigationJournal _navigationJournal;

        public MainViewModel(IRegionManager regionManager) : this()
        {
            _regionManager = regionManager;
        }

        public MainViewModel()
        {

        }

        #region 导航

        /// <summary>
        /// 导航 - 命令
        /// </summary>
        private DelegateCommand<MenuBar> _navigateCmd;

        /// <summary>
        /// 导航 - 命令
        /// </summary>
        public DelegateCommand<MenuBar> NavigateCmd =>
            _navigateCmd ?? (_navigateCmd = new DelegateCommand<MenuBar>(ExecuteNavigate));

        /// <summary>
        /// 执行 - 导航
        /// </summary>    
        void ExecuteNavigate(MenuBar parameter)
        {
            if (parameter != null && !string.IsNullOrEmpty(parameter?.NameSpace))
            {
                _regionManager?.Regions[PrismManager.MainViewRegionName]?.RequestNavigate(parameter.NameSpace, callBack =>
                {
                    _navigationJournal = callBack.Context.NavigationService.Journal;
                });
            }
        }

        #endregion 导航

        #region 返回

        /// <summary>
        /// 返回 - 命令
        /// </summary>
        private DelegateCommand _backCmd;

        /// <summary>
        /// 返回 - 命令
        /// </summary>
        public DelegateCommand BackCmd =>
            _backCmd ?? (_backCmd = new DelegateCommand(ExecuteBack));

        /// <summary>
        /// 执行 - 返回
        /// </summary>    
        void ExecuteBack()
        {
            if (_navigationJournal?.CanGoBack == true)
            {
                _navigationJournal?.GoBack();
            }
        }

        #endregion 返回

        #region 前进

        /// <summary>
        /// 前进 - 命令
        /// </summary>
        private DelegateCommand _forwardCmd;

        /// <summary>
        /// 前进 - 命令
        /// </summary>
        public DelegateCommand ForwardCmd =>
            _forwardCmd ?? (_forwardCmd = new DelegateCommand(ExecuteForward));

        /// <summary>
        /// 执行 - 前进
        /// </summary>    
        void ExecuteForward()
        {
            if (_navigationJournal?.CanGoForward == true)
            {
                _navigationJournal?.GoForward();
            }
        }

        #endregion 前进

        #region 菜单列表

        /// <summary>
        /// 菜单列表
        /// </summary>
        private ObservableCollection<MenuBar> _menuBars;

        /// <summary>
        /// 菜单列表
        /// </summary>
        public ObservableCollection<MenuBar> MenuBars
        {
            get { return _menuBars; }
            set { SetProperty(ref _menuBars, value); }
        }

        #endregion 菜单列表 

        /// <summary>
        /// 创建菜单栏
        /// </summary>
        void CreateMenuBars()
        {
            MenuBars = new ObservableCollection<MenuBar>();
            MenuBars.Add(new MenuBar()
            {
                Icon = "Home",
                Title = "首页",
                NameSpace = "IndexView",
            });

            MenuBars.Add(new MenuBar()
            {
                Icon = "NotebookOutline",
                Title = "待办事项",
                NameSpace = "ToDoView",
            });

            MenuBars.Add(new MenuBar()
            {
                Icon = "Note",
                Title = "备忘录",
                NameSpace = "MemoView",
            });

            MenuBars.Add(new MenuBar()
            {
                Icon = "Cog",
                Title = "设置",
                NameSpace = "SettingsView",
            });
        }

        /// <summary>
        /// 配置首页初始化参数
        /// </summary>
        public void Configure()
        {
            CreateMenuBars();
            _regionManager?.Regions[PrismManager.MainViewRegionName]?.RequestNavigate("IndexView", callBack =>
            {
                _navigationJournal = callBack.Context.NavigationService.Journal;
            });
        }
    }
}
